import 'package:flutter/animation.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

void main() {
  runApp(const MaterialApp(home: HomePage()));
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin {
  late AnimationController _controlier;
  @override
  void initState() {
    super.initState();
    _controlier = AnimationController(
      vsync: this, // 程序和设备刷新率统一
      duration: const Duration(seconds: 1),
      lowerBound: 0, // 相当于起始状态
      upperBound: 1, // 相当于最大状态是默认状态的倍数
    );
    // ..repeat(reverse: true);

    // 监听动画执行状态
    _controlier.addListener(() {
      // print(_controlier.value); // 0-1
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('你好flutter1'),
        ),
        body: Column(
          children: [
            Row(
              children: [
                // 1 手动控制旋转
                RotationTransition(
                  turns: _controlier,
                  child: FlutterLogo(size: 60),
                ),
                // 2 手动透明度
                FadeTransition(
                  opacity: _controlier,
                  child: FlutterLogo(size: 60),
                ),
                // 3 手动缩放
                ScaleTransition(
                  // 运动范围，和  lowerBound   upperBound 一样效果
                  scale: _controlier.drive(
                    Tween(begin: 0.5, end: 1),
                  ),
                  child: FlutterLogo(size: 60),
                ),
              ],
            ),
            Row(
              children: [
                // 4 手动控制滑动
                SlideTransition(
                  // position: _controlier.drive(Tween(
                  //   begin: const Offset(0, 0),
                  //   end: const Offset(1, 0), // 默认向右位移到宽度100*1的位置
                  // )),
                  // 等同于上面
                  position: Tween(
                    begin: const Offset(0, 0),
                    end: const Offset(1, 0),
                  )
                      // 运动轨迹
                      .chain(CurveTween(curve: Curves.ease))
                      // 交错动画 总运动时间的前20%前暂停，20%-80%运动 80%后暂停
                      .chain(CurveTween(curve: const Interval(0.2, 0.8)))
                      .animate(_controlier),
                  child: Container(
                    decoration: BoxDecoration(
                      border: Border.all(color: Colors.red),
                    ),
                    child: const FlutterLogo(size: 60),
                  ),
                )
              ],
            ),
            Wrap(
              spacing: 10,
              runSpacing: 5,
              alignment: WrapAlignment.spaceAround,
              children: [
                ElevatedButton(
                  onPressed: () {
                    _controlier.repeat(reverse: true);
                  },
                  child: Text('无限执行'),
                ),
                ElevatedButton(
                  onPressed: () {
                    _controlier.stop();
                  },
                  child: Text('停止'),
                ),
                ElevatedButton(
                  onPressed: () {
                    _controlier.forward();
                  },
                  child: Text('执行一次'),
                ),
                ElevatedButton(
                  onPressed: () {
                    _controlier.reverse();
                  },
                  child: Text('反向执行一次'),
                ),
                ElevatedButton(
                  onPressed: () {
                    _controlier.reset();
                  },
                  child: Text('重置'),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}
